import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  ParseIntPipe,
  Patch,
  Post,
  Query,
  UseGuards,
} from '@nestjs/common';

import { UserRepository } from './user.repository';
// import { AuthGuard } from '@nestjs/passport';
import { AdminGuard } from '@/common/guards/admin.guard';
import { JwtGuard } from '@/common/guards/jwt.guard';
import { Public } from '@/common/decorators/public.decorators';
import {
  Permission,
  Read,
  Update,
  Create,
} from '@/common/decorators/role-permission.decorators';
import { RolePermissionGuard } from '@/common/guards/role-permission.guard';
import { CreateUserDto } from './dto/create-user.dto';
import { Serialize } from '@/common/decorators/serialize.decorator';
import { PublicUserDto } from '@/auth/dto/public-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { PublicUpdateUserDto } from './dto/public-update-user.dto';
import { AuthGuard } from '@nestjs/passport';
import { PolicyGuard } from '@/common/guards/policy.guard';

@Controller('user')
@UseGuards(AuthGuard('jwt'), RolePermissionGuard, PolicyGuard)
// @UseGuards(JwtGuard, AdminGuard) // 自定义jwt
@Permission('user')
export class UserController {
  constructor(private userRepository: UserRepository) {}
  @Post()
  @Serialize(PublicUserDto)
  create(@Body() createUserDto: CreateUserDto) {
    return this.userRepository.create(createUserDto);
  }

  @Get()
  @Read()
  // @Update()
  findAll(
    @Query('page', new ParseIntPipe({ optional: true })) page,
    @Query('limit', new ParseIntPipe({ optional: true })) limit,
  ) {
    return this.userRepository.findAll(page, limit);
  }
  @Get(':username')
  @Serialize(PublicUserDto)
  findOne(@Param('username') username: string) {
    return this.userRepository.findOne(username);
  }

  @Patch()
  @Serialize(PublicUpdateUserDto)
  // @Update()
  updata(@Body() updateUserDto: UpdateUserDto) {
    return this.userRepository.update(updateUserDto);
    // return updateUserDto;
  }

  @Delete(':id')
  delete(@Param('id') id: string) {
    return this.userRepository.delete(+id);
  }
}
